package com.example.document

import android.content.Intent
import android.util.Log
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.document.databinding.ActivityDocBinding
import com.example.lib_adapter.BaseQuickAdapter
import com.example.lib_base.BaseActivity
import com.example.lib_common.swipereveallayout.SwipeRevealLayout
import kotlinx.coroutines.launch

@Route(path = "/doc/DocActivity")
class DocActivity : BaseActivity<ActivityDocBinding,DocViewModel> (){
    val docAdapter by lazy { DocAdapter(R.layout.item_doc) }

    override fun createVM(): DocViewModel {
        return ViewModelProvider(this).get(DocViewModel::class.java)
    }

    override fun createV(): ActivityDocBinding {
        return ActivityDocBinding.inflate(layoutInflater)
    }

    override fun bindState() {
        lifecycleScope.launch {
            vm.mutableContainer.uiFlow.collect{
                val state = it as DocState
                if(state.docState){
                   Log.i("===",state.data.toString())
                    docAdapter.addAll(state.data)
                }else if(state.updState){
                    Toast.makeText(this@DocActivity, "上传成功", Toast.LENGTH_SHORT).show()
                    vm.selDocuments()
                }
            }
        }
    }

    override fun initData() {
        vm.selDocuments()
    }

    override fun initView() {
        vb.docBack.setOnClickListener{
            finish()
        }

        vb.btnUp.setOnClickListener {
            ARouter.getInstance().build("/doc/LocalActivity").navigation(this,100)
        }

        vb.docRv.apply {
            layoutManager=LinearLayoutManager(this@DocActivity)
            adapter=docAdapter
        }

        docAdapter.setOnItemClickListener(object :BaseQuickAdapter.OnItemClickListener{
            override fun onItemClick(position: Int) {
                Toast.makeText(this@DocActivity, docAdapter.data.get(position).fileName, Toast.LENGTH_SHORT).show()
            }

        })
        docAdapter.setOnItemLongClickListener(object :BaseQuickAdapter.OnItemLongClickListener{
            override fun onItemLongClick(position: Int): Boolean {
                Toast.makeText(
                    this@DocActivity,
                    docAdapter.data.get(position).filePath,
                    Toast.LENGTH_SHORT
                ).show()
                return true
            }
        })

        docAdapter.setOnItemChildClickListaner(object :BaseQuickAdapter.OnItemChildClickListener{
            override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, id: Int, position: Int) {
                if(id==R.id.doc_delete){
                    /*Toast.makeText(this@DocActivity, ""+position, Toast.LENGTH_SHORT).show()
                    docAdapter.removeData(position)*/
                    vm.delDocument(docAdapter.getData().get(position).fileId)
                    vb.docRv.closeMenu()
                    /*val view = adapter.getViewHolder().getView<SwipeRevealLayout>(R.id.doc_swipe)
                    view.getSwipeListener().onClosed(view)*/
                }
            }

        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==100 && resultCode== RESULT_OK){
            val bundle = data?.extras
            if(bundle!=null){
                val name = bundle.getString("name")
                val path = bundle.getString("path")
                Log.i("===回传",name+","+path)
                vm.uploadDoc(path!!,name!!)
            }
        }
    }
}